<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="search">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
          <el-form-item label="标题" prop="userName">
            <el-input v-model="queryParams.title" placeholder="请输入用户名称" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-select v-model="queryParams.type" placeholder="类型" clearable>
              <el-option v-for="dict in helpful" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>
    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button v-hasPermi="['system:helpInfo:add']" type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" :search="true" @query-table="getList"></right-toolbar>
        </el-row>
      </template>
      <el-table v-loading="loading" :data="helpList">
        <el-table-column label="标题" align="center" prop="title" />
        <el-table-column label="类型" align="center" prop="type">
          <template #default="scope">
            <dict-tag :options="helpful" :value="scope.row.type" />
          </template>
        </el-table-column>
        <el-table-column label="排序" align="center" prop="sort" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template #default="scope">
            <span>{{ scope.row.createTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="编辑" placement="top">
              <el-button v-hasPermi="['system:helpInfo:edit']" link type="primary" icon="Edit" @click="handleUpdate(scope.row)"> </el-button>
            </el-tooltip>
            <el-tooltip content="详情" placement="top">
              <el-button link type="primary" icon="SuitcaseLine" @click="handleDetail(scope.row)"> </el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button v-hasPermi="['system:helpInfo:remove']" link type="primary" icon="Delete" @click="handleDelete(scope.row)"> </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <el-drawer v-model="drawer.visible" :title="drawer.title" destroy-on-close append-to-body size="50%">
      <el-form ref="helpFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" filterable placeholder="请选择类型" clearable>
            <el-option v-for="item in helpful" :key="item.label" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序数" />
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.helpInfoDetails.message" :min-height="200" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-drawer>
    <el-drawer v-model="drawer1" title="详情" destroy-on-close append-to-body size="50%">
      <el-form :model="form" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="标题:">{{ form.title }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="类型:">
              <dict-tag :options="helpful" :value="form.type" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="排序:">{{ form.sort }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容:">
              <editor v-model="form.helpInfoDetails.message" :min-height="200" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="cancel">确 定</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>
<script setup lang="ts">
import { getHelpInfoList, getHelpInfoDetail, addHelpInfo, updateHelpInfo, deleteHelpInfo } from '@/api/help';
import { HelpInfoVO, HelpInfoQuery, HelpInfoForm } from '@/api/help/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { helpful } = toRefs<any>(proxy?.useDict('helpful'));

const drawer = reactive<DialogOption>({
  visible: false,
  title: ''
});
const drawer1 = ref(false);

const loading = ref(true);
const showSearch = ref(true);
const queryFormRef = ref<ElFormInstance>();
const helpFormRef = ref<ElFormInstance>();
const helpList = ref<HelpInfoVO[]>([]);
const total = ref(0);

const initFormData: HelpInfoForm = {
  id: undefined,
  type: undefined,
  title: undefined,
  sort: undefined,
  helpInfoDetails: {
    id: undefined,
    helpId: undefined,
    message: undefined
  }
};

const data = reactive<PageData<HelpInfoForm, HelpInfoQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    type: null,
    title: null
  },
  rules: {
    title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
    type: [{ required: true, message: '类型不能为空', trigger: 'change' }],
    sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }]
  }
});
const { form, queryParams, rules } = toRefs(data);

/**
 * 查询帮助中心列表
 */
const getList = async () => {
  loading.value = true;
  const res = await getHelpInfoList(queryParams.value);
  helpList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  drawer.visible = false;
  drawer1.value = false;
};
/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  helpFormRef.value?.resetFields();
};

/**
 * 搜索按钮操作
 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置 */
const resetQuery = () => {
  queryParams.value.title = null;
  queryParams.value.type = null;
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 新增按钮操作 */
const handleAdd = async () => {
  reset();
  drawer.visible = true;
  drawer.title = '新增文章';
};

/** 修改按钮操作 */
const handleUpdate = async (row: HelpInfoVO) => {
  reset();
  const res = await getHelpInfoDetail(row.id);
  form.value = res.data;
  drawer.visible = true;
  drawer.title = '修改文章';
};

/** 提交按钮 */
const submitForm = () => {
  helpFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      form.value.id ? await updateHelpInfo(form.value) : await addHelpInfo(form.value);
      proxy?.$modal.msgSuccess('操作成功');
      drawer.visible = false;
      await getList();
    }
  });
};

/** 商品详情 */
const handleDetail = async (row: HelpInfoVO) => {
  reset();
  const res = await getHelpInfoDetail(row.id);
  form.value = res.data;
  drawer1.value = true;
};

/** 删除按钮操作 */
const handleDelete = async (row: HelpInfoVO) => {
  await proxy?.$modal.confirm('是否确认删除名称为"' + row.title + '"的数据项?');
  await deleteHelpInfo(row.id);
  await getList();
  proxy?.$modal.msgSuccess('删除成功');
};

onMounted(() => {
  getList();
});
</script>

<style scoped lang="scss"></style>
